package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.ExceptionaLabor;
import com.ylxl.entity.ExceptionaLaborDetail;
import com.ylxl.entity.reponse.ExceptionaLaborVO;
import com.ylxl.entity.request.ExceptionaLaborCmdOrMdy;
import com.ylxl.entity.request.QueryExceptionaLabor;
import com.ylxl.entity.request.StatusRequest;
import com.ylxl.mapper.ExceptionaLaborDetailMapper;
import com.ylxl.mapper.ExceptionaLaborMapper;
import com.ylxl.service.IExceptionaLaborService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户 业务层处理
 *
 */
@Service
public class ExceptionaLaborServiceImpl extends ServiceImpl<ExceptionaLaborMapper, ExceptionaLabor>  implements IExceptionaLaborService {
    private static final Logger log = LoggerFactory.getLogger(ExceptionaLaborServiceImpl.class);

    @Autowired
    private ExceptionaLaborMapper exceptionaLaborMapper;
    @Resource
    private ExceptionaLaborDetailMapper exceptionaLaborDetailMapper;

    @Override
    public List<ExceptionaLabor> selectLabelList(QueryExceptionaLabor queryExceptionaLabor) {
        LambdaQueryWrapper<ExceptionaLabor> exceptionaLaborLambdaQueryWrapper = new LambdaQueryWrapper<>();
        exceptionaLaborLambdaQueryWrapper
                .like(StringUtils.isNotEmpty(queryExceptionaLabor.getDocumentsNo()),ExceptionaLabor::getDocumentsNo,queryExceptionaLabor.getDocumentsNo())
                .like(StringUtils.isNotEmpty(queryExceptionaLabor.getApplicant()),ExceptionaLabor::getApplicant,queryExceptionaLabor.getApplicant())
                .like(StringUtils.isNotEmpty(queryExceptionaLabor.getApplicantCode()),ExceptionaLabor::getApplicantCode,queryExceptionaLabor.getApplicantCode())
                .like(StringUtils.isNotEmpty(queryExceptionaLabor.getApplicantCompany()),ExceptionaLabor::getApplicantCompany,queryExceptionaLabor.getApplicantCompany())
                .like(StringUtils.isNotEmpty(queryExceptionaLabor.getApplicantDepartment()),ExceptionaLabor::getApplicantDepartment,queryExceptionaLabor.getApplicantDepartment())
                .orderByDesc(ExceptionaLabor::getApplicantTime)
        ;
        List<ExceptionaLabor> exceptionaLabors = exceptionaLaborMapper.selectList(exceptionaLaborLambdaQueryWrapper);
        return exceptionaLabors;
    }

    @Override
    public int insertExceptionaLabor(ExceptionaLaborCmdOrMdy exceptionaLaborCmdOrMdy) {
        ExceptionaLabor exceptionaLaborInsert = new ExceptionaLabor();
        BeanUtil.copyProperties(exceptionaLaborCmdOrMdy, exceptionaLaborInsert);//属性拷贝
        int insert = exceptionaLaborMapper.insert(exceptionaLaborInsert);
        List<ExceptionaLaborDetail> exceptionaLaborDetailList = exceptionaLaborCmdOrMdy.getExceptionaLaborDetailList();
        if(CollectionUtil.isNotEmpty(exceptionaLaborDetailList)){
            exceptionaLaborDetailList.forEach(v->{
                ExceptionaLaborDetail exceptionaLaborDetail = new ExceptionaLaborDetail();
                exceptionaLaborDetail.setExceptionId(exceptionaLaborInsert.getId());
                BeanUtil.copyProperties(v, exceptionaLaborDetail);//属性拷贝
                exceptionaLaborDetailMapper.insert(exceptionaLaborDetail);
            });
        }
        return insert;
    }

    @Override
    public int updateExceptionaLabor(ExceptionaLaborCmdOrMdy exceptionaLaborCmdOrMdy) {
        if(StringUtils.isEmpty(exceptionaLaborCmdOrMdy.getId())){
            throw new BaseException("例外劳保id不能为空");
        }
        ExceptionaLabor exceptionaLaborUpdate = new ExceptionaLabor();
        BeanUtil.copyProperties(exceptionaLaborCmdOrMdy, exceptionaLaborUpdate);//属性拷贝
        int update = exceptionaLaborMapper.updateById(exceptionaLaborUpdate);
        exceptionaLaborDetailRemove(exceptionaLaborCmdOrMdy.getId());
        List<ExceptionaLaborDetail> exceptionaLaborDetailList = exceptionaLaborCmdOrMdy.getExceptionaLaborDetailList();
        if(CollectionUtil.isNotEmpty(exceptionaLaborDetailList)){
            exceptionaLaborDetailList.forEach(v->{
                ExceptionaLaborDetail exceptionaLaborDetail = new ExceptionaLaborDetail();
                exceptionaLaborDetail.setExceptionId(exceptionaLaborCmdOrMdy.getId());
                BeanUtil.copyProperties(v, exceptionaLaborDetail);//属性拷贝
                exceptionaLaborDetailMapper.insert(exceptionaLaborDetail);
            });
        }
        return update;
    }

    @Override
    public ExceptionaLaborVO selctById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("例外劳保id不能为空");
        }
        ExceptionaLabor exceptionaLabor = exceptionaLaborMapper.selectById(id);
        ExceptionaLaborVO exceptionaLaborVO = new ExceptionaLaborVO();
        BeanUtil.copyProperties(exceptionaLabor, exceptionaLaborVO);//属性拷贝

        LambdaQueryWrapper<ExceptionaLaborDetail> exceptionaLaborDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        exceptionaLaborDetailLambdaQueryWrapper.eq(ExceptionaLaborDetail::getExceptionId,id);
        List<ExceptionaLaborDetail> exceptionaLaborDetails = exceptionaLaborDetailMapper.selectList(exceptionaLaborDetailLambdaQueryWrapper);
        exceptionaLaborVO.setExceptionaLaborDetailList(exceptionaLaborDetails);
        return exceptionaLaborVO;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        LambdaUpdateWrapper<ExceptionaLaborDetail> exceptionaLaborDetailLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        exceptionaLaborDetailLambdaUpdateWrapper.in(ExceptionaLaborDetail::getExceptionId,ids);
        exceptionaLaborDetailMapper.delete(exceptionaLaborDetailLambdaUpdateWrapper);
        return exceptionaLaborMapper.deleteBatchIds(ids);
    }

    @Override
    public List<ExceptionaLaborDetail> selctByDetailId(Integer laborId) {
        LambdaQueryWrapper<ExceptionaLaborDetail> exceptionaLaborDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        exceptionaLaborDetailLambdaQueryWrapper.eq(ExceptionaLaborDetail::getExceptionId,laborId);
        List<ExceptionaLaborDetail> exceptionaLaborDetails = exceptionaLaborDetailMapper.selectList(exceptionaLaborDetailLambdaQueryWrapper);
        return exceptionaLaborDetails;
    }

    @Override
    public int enableOrDisable(StatusRequest statusRequest) {
        ExceptionaLabor exceptionaLabor = new ExceptionaLabor();
        BeanUtil.copyProperties(statusRequest, exceptionaLabor);//属性拷贝
        return exceptionaLaborMapper.updateById(exceptionaLabor);
    }

    public void exceptionaLaborDetailRemove(Integer exceptionId){
        LambdaUpdateWrapper<ExceptionaLaborDetail> exceptionaLaborDetailLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        exceptionaLaborDetailLambdaUpdateWrapper.eq(ExceptionaLaborDetail::getExceptionId,exceptionId);
        exceptionaLaborDetailMapper.delete(exceptionaLaborDetailLambdaUpdateWrapper);
    }
}
